package org.lzy.shop.network

object NetworkStateManager {
    private val listeners = mutableListOf<NetworkStateListener>()
    var isNetworkAvailable = false

    // 注册网络状态监听
    fun registerListener(listener: NetworkStateListener) {
        if (!listeners.contains(listener)) {
            listeners.add(listener)
            // 立即通知当前状态
            listener.onNetworkStateChanged(isNetworkAvailable)
        }
    }

    // 取消注册
    fun unregisterListener(listener: NetworkStateListener) {
        listeners.remove(listener)
    }

    // 更新网络状态
    fun updateNetworkState(available: Boolean) {
        isNetworkAvailable = available
        for (listener in listeners) {
            listener.onNetworkStateChanged(available)
        }
    }

    interface NetworkStateListener {
        fun onNetworkStateChanged(isAvailable: Boolean)
    }
}